package com.ilop.sthome.ui.adapter.device;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.bean.AlarmNotice;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-01-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：网关推送消息设置
 */
public class DeviceSetNoticeAdapter extends RecyclerView.Adapter<DeviceSetNoticeAdapter.ItemHolder> {

    private Context mContext;
    private List<AlarmNotice> mList;

    public DeviceSetNoticeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<AlarmNotice> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_set_up_notice, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        AlarmNotice alarm = mList.get(i);
        switch (i){
            case 0:
                itemHolder.mName.setText(mContext.getString(R.string.gateway_online_push));
                itemHolder.setVisibility(true);
                break;
            case 1:
                itemHolder.mName.setText(mContext.getString(R.string.gateway_offline_push));
                itemHolder.setVisibility(true);
                break;
            default:
                itemHolder.setVisibility(false);
                break;
        }
        itemHolder.mState.setImageResource(alarm.isNoticeEnabled() ? R.mipmap.btn_on_48 : R.mipmap.btn_off_48);

        itemHolder.mState.setOnClickListener(v -> LiveDataBus.get().with(CommonId.ALARM_NOTICE).setValue(alarm));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_set_up_name)
        TextView mName;
        @BindView(R.id.item_set_up_state)
        ImageView mState;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setVisibility(boolean visible) {
            RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if (visible) {
                param.height = LinearLayout.LayoutParams.WRAP_CONTENT; // 根据具体需求场景设置
                param.width = LinearLayout.LayoutParams.MATCH_PARENT;
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView.setVisibility(View.GONE);
                param.height = 1;
                param.width = 0;
            }
            itemView.setLayoutParams(param);
        }
    }
}
